<?php

namespace app\admin\controller\mk;
use app\common\controller\Backend;
use app\admin\model\mk\SeckillPro;

/**
 * 秒杀
 */
class Seckill extends Backend
{
    /**
     * Seckill模型对象
     * @var object
     * @phpstan-var \app\admin\model\mk\Seckill
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['id'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\mk\Seckill();
        $this->request->filter('clean_xss');
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */

    function pro(){
          $act = input("post.act");
          $data = input("post.data");
          switch ($act) {
              case 'save':
                  $data['status'] = $data['status'] ? 1 : 0;
                  if($data['pro_id'] == '' || $data['pro_id'] == 0){
                       $this->success("error",['msg'=>'请选择商品','code'=>0]);
                  }
                  if($data['seckill_price'] == ''){
                       $this->success("error",['msg'=>'秒杀价必须填写','code'=>0]);
                  }
                  if($data['seckill_stock'] == ''){
                       $this->success("error",['msg'=>'秒杀库存必须填写','code'=>0]);
                  }
                  $data['seckill_id'] = intval(input("post.id"));
                  SeckillPro::insert($data);
                  $this->success("ok",['code'=>1]);
                  break;
              case 'delete':
                 $id = input("post.id");
                 if($id){
                       $dd = SeckillPro::where("id",$id)->delete();
                       if($dd){
                              $this->success("1",['msg'=>'ok','code'=>1]);
                       }else{
                              $this->success("1",['msg'=>'失败','code'=>0]);
                       }
                 }
                 break;
              case 'prolist':
                   $id = input("post.id");
                   $res = SeckillPro::alias("s")->where("s.seckill_id",$id)->join("goods p","p.id=s.pro_id","LEFT")->field("p.title,p.price,p.feng,s.*")->select();
                   $this->success("ok",$res);
                   break; 
              default:
                  // code...
                  break;
          }
    }
}